<?php
/** Retina Get Image Id */
function retina_get_image_id( $num = 0 ) {
	global $post;

	$image_ids = array_keys(
		get_children(
			array(
				'post_parent' => $post->ID,
				'post_type' => 'attachment',
				'post_mime_type' => 'image',
				'orderby' => 'menu_order',
				'order' => 'ASC'
			)
		)
	);

	if ( isset( $image_ids[$num] ) ) {
		return $image_ids[$num];
	}

	return false;
}

/** Retina Get Image*/
function retina_get_image( $args = array() ) {
	
	global $post;

	/** Arguments */
	$defaults = array( 'format' => 'html', 'size' => 'full', 'num' => 0, 'attr' => '' );	
	$defaults = apply_filters( 'retina_get_image_defaults', $defaults );	
	$args = wp_parse_args( $args, $defaults );

	/** WordPress built-in method */
	if ( has_post_thumbnail() && ( $args['num'] === 0 ) ) {
		
		$id = get_post_thumbnail_id();
		$html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
		list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
	
	}
	
	/** Grab the first attachment image */		
	else {
		
		// $id = retina_get_image_id( $args['num'] );
		// $html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
		// list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );


        // wirrten by crifan
        // get the first pic url if exist
        
        $postId = get_the_id();
        $curPost = get_post($postId);
        //$content = $curPost['post_content']; // not work here !!!
        $content = $curPost->post_content; 
        //$content = the_content(); // not work !!!, it will output all content !!!
        
        # current support valid pic suffiex: 'bmp', 'gif', 'jpeg', 'jpg', 'jpe', 'png', 'tiff', 'tif'
        /* $picSufChars = "befgijmnptBEFGIJMNPT"; */
        /* $pattern = '#<a href="(?P<picUrl>http://storage.live.com/items/.+?filename=.+?\.jpg)"><img.+?src="(?P=picUrl)".+?/></a>#'; */
        /* $pattern = '#<a(\s+?\w+="?.+?"?)*? href="(?P<picUrl>http://storage.live.com/items/.+?filename=.+?\.['.$picSufChars.']{3,4})"(\s+?\w+="?.+?"?)*?><img.+?src="(?P=picUrl)".+?/></a>#'; */
        
        /* $pattern = '#<a(\s+?\w+="?.+?"?)*? href="(?P<picUrl>http://storage.live.com/items/.+?)"(\s+?\w+="?.+?"?)*?><img(\s+?\w+="?.+?"?)*?\s*?src="(?P=picUrl)"(\s+?\w+="?.+?"?)*?\s*?/></a>#'; */
        
        $pattern = '#<a href="(?P<picUrl>http://storage.live.com/items/.+?)"(\s+?\w+="?.+?"?)*?><img(\s+?\w+="?.+?"?)*?\s*?src="(?P=picUrl)"(\s+?\w+="?.+?"?)*?\s*?/></a>#';
       
        if(preg_match($pattern, $content, $matches)){
            $picUrl = $matches["picUrl"];
            //print "found picUrl=".$picUrl;
            if(preg_match('#http://storage\.live\.com/items/.+?filename=(?P<filename>.+)#', $picUrl, $foundFilename)){
                $filename = $foundFilename["filename"];
                $filename = rawurldecode($filename);
            }
            else{
                $filename = "";
            }
            
            //<a href="http://www.crifan.com/wordpress_importer_import_wxr_error_there_has_been_an_error_php_ini_post_max_size_upload_max_filesize/" title="【已解决】WordPress Importer导入WXR文件出错：Sorry, there has been an error.文件是空的。请上传有内容的文件。这个错误也有可能是因为您的 php.ini 禁止了上传，或其中 post_max_size 的值小于 upload_max_filesize 的值"><img width="150" height="109" src="http://www.crifan.com/wp-content/uploads/2012/04/restart-apache-150x109.jpg" class="entry-image" alt="restart-apache.jpg" title="restart-apache.jpg" /></a>
            
            //$picHtml = sprintf('<a href="%1$s" title="%2$s"><img width="150" height="109" src="%3$s" class="entry-image" alt="%4$s" title="%5$s" /></a>', get_permalink(), get_the_title(), $picUrl, $filename, $filename);
            //$picHtml = sprintf('<a href="%1$s" title="%2$s"><img width="150" src="%3$s" class="entry-image" alt="%4$s" title="%5$s" /></a>', get_permalink(), get_the_title(), $picUrl, $filename, $filename);
            //$picHtml = sprintf('<a href="%1$s" title="%2$s"><img height="109" src="%3$s" class="entry-image" alt="%4$s" title="%5$s" /></a>', get_permalink(), get_the_title(), $picUrl, $filename, $filename);
            $picHtml = sprintf('<a href="%1$s" title="%2$s"><img width="150" height="150" src="%3$s" class="entry-image" alt="%4$s" title="%5$s" /></a>', get_permalink(), get_the_title(), $picUrl, $filename, $filename);
        } else {
            $picUrl = "";
            $picHtml = "";
        }

        $id = false;
        $html = $picHtml;
        $url = $picUrl;
	}
    
	/** Source path, relative to the root */
	$src = str_replace( home_url(), '', $url );

	/** Output Logic */
	if ( strtolower( $args['format'] ) == 'html' ) {
		$output = $html;
	} else if ( strtolower( $args['format'] ) == 'url' ) {
		$output = $url;
	} else {
		$output = $src;
	}

	/** return FALSE if $url is blank */
	if ( empty( $url ) ) {
		$output = FALSE;
	}

	/** return data, filtered */
	return apply_filters( 'retina_get_image_output', $output, $args, $id, $html, $url, $src );
}

/** Retina Image Sizes */
function retina_get_image_sizes() {
	
	$default_sizes = array(
		
		'none'		=> array(
			'width' => 0,
			'height' => 0
		),
		
		'large'		=> array(
			'width' => get_option('large_size_w'),
			'height' => get_option('large_size_h')
		),
		
		'medium'	=> array(
			'width' => get_option('medium_size_w'),
			'height' => get_option('medium_size_h')
		),
		
		'thumbnail'	=> array(
			'width' => get_option('thumbnail_size_w'),
			'height' => get_option('thumbnail_size_h')
		)
	);

	$additional_sizes = retina_get_additional_image_sizes();	
	return array_merge( $default_sizes, $additional_sizes );
}

/** Retina Additional Image Sizes */
function retina_get_additional_image_sizes() {
	global $_wp_additional_image_sizes;

	if ( ! empty( $_wp_additional_image_sizes ) ) {
		return $_wp_additional_image_sizes;
	}

	return array();
}
?>